#include "RepeationTest.h"
#include "../suduku_without_gui/SudokuReader.h"

using namespace std;
namespace SudokuProblemMaker
{
	
	void openFileTest(wchar_t* in_path, wchar_t* out_path)
	{
		h_sudoku_problem_txt = CreateFile(
			in_path,
			GENERIC_READ,
			0,
			NULL,
			OPEN_EXISTING,
			FILE_ATTRIBUTE_NORMAL,
			NULL
		);

		h_sudoku_txt = CreateFile(
			out_path,
			GENERIC_READ | GENERIC_WRITE,
			0,
			NULL,
			CREATE_ALWAYS,
			FILE_ATTRIBUTE_NORMAL,
			NULL
		);
	}


	void zeroElementTest(Sudoku& s)
	{
		int num_zero = rand() % 81 + 1;
		while (num_zero < 30)
		{
			num_zero = rand() % 81 + 1;
		}
		for (int i = 0; i < num_zero; i++)
		{
			int row_idx = rand() % 9 + 1;
			int col_idx = rand() % 9 + 1;
			if (s[row_idx][col_idx] == 0) continue;
			s[row_idx][col_idx] = 0;
		}
	}

	void makeProblemTest(wchar_t* in_path, wchar_t* out_path)
	{
		openFileTest(in_path, out_path);
		bool is_end_test = false;
		int num_in_read_buff;
		judgeCRLF();
		for (int i = 0; i < BUFF_SIZE; i++)
			mallocSudoku(read_buff[i]);
		do 
		{
			readSudoku(is_end_test, num_in_read_buff);
			if (!is_end_test)
				for (int i = 0; i < num_in_read_buff; i++)
				{
					zeroElementTest(read_buff[i]);
					printSudoku(read_buff[i], false);
				}
			else
			{
				for (int i = 0; i < num_in_read_buff - 1; i++)
				{
					zeroElementTest(read_buff[i]);
					printSudoku(read_buff[i], false);
				}
				zeroElementTest(read_buff[num_in_read_buff - 1]);
				printSudoku(read_buff[num_in_read_buff - 1], true);
				freeSudoku(read_buff[num_in_read_buff - 1]);
			}
		} while (!is_end_test);
		for (int i = 0; i < BUFF_SIZE; i++)
			mallocSudoku(read_buff[i]);
		return;
	}

}

void makeSudokuProblem(wchar_t* in_path, wchar_t* out_path)
{
	SudokuProblemMaker::makeProblemTest(in_path, out_path);
}